﻿using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Mask;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CompanyManageSystem.DLL
{
    public class PageControl : XtraUserControl
    {
        private int _pageCount;
        private int _pageIndex;
        private int _pageSize;
        private int _totalCount;

        //共0条
        private LabelControl labelControl1;
        //共0页
        private LabelControl labelControl2;

        //显示当前页
        private TextEdit textEdit1;

        //跳转
        private SimpleButton btn_Jump;

        //上一页
        private SimpleButton btn_Prev;
        //下一页
        private SimpleButton btn_Next;
        //首页
        private SimpleButton btn_First;
        //尾页
        private SimpleButton btn_Last;

        private LabelControl labelControl3;
        private ComboBoxEdit comboBoxEdit1;
        private LabelControl labelControl4;

        /// <summary>
        /// 总页数
        /// </summary>
        public int PageCount
        {
            get
            {
                return this._pageCount;
            }
            set
            {
                this._pageCount = value;
                this.labelControl2.Text = "共 " + this._pageCount + " 页";                
            }
        }
        /// <summary>
        /// 当前页码
        /// </summary>
        public int PageIndex
        {
            get
            {
                return this._pageIndex;
            }
            set
            {
                this._pageIndex = value;
                this.textEdit1.Text = this._pageIndex.ToString();
                this.btn_First.Enabled = true;
                this.btn_Prev.Enabled = true;
                this.btn_Next.Enabled = true;
                this.btn_Last.Enabled = true;
                if ((this.PageIndex == 1) || (this.PageCount == 0))
                {
                    this.btn_First.Enabled = false;
                    this.btn_Prev.Enabled = false;
                }
                if ((this.PageIndex >= this.PageCount) || (this.PageCount == 0))
                {
                    this.btn_Next.Enabled = false;
                    this.btn_Last.Enabled = false;
                }
            }


        }
        /// <summary>
        /// 每页显示信息数
        /// </summary>
        public int PageSize
        {
            get
            {
                return this._pageSize;
            }
            set
            {
                this._pageSize = value;
                this.comboBoxEdit1.Text = this._pageSize.ToString();
            }
        }
        /// <summary>
        /// 信息总数
        /// </summary>
        public int TotalCount
        {
            get
            {
                return this._totalCount;
            }
            set
            {
                this._totalCount = value;
                //this.PageCount = (this._pageSize == 0) ? 0 : Convert.ToInt32(Math.Ceiling((double)(((double)this.TotalCount) / ((double)this._pageSize))));
                this.labelControl1.Text = "共 " + this._totalCount + " 条";
            }
        }

        /// <summary>
        /// 刷新事件
        /// </summary>
        public event EventHandler<EventArgs> DataRefresh;
        private IContainer components;

        public PageControl()
        {
            this.components = null;
            this.InitializeComponent();
            this.PageIndex = 1;
        }

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(PageControl));
            this.btn_Prev = new DevExpress.XtraEditors.SimpleButton();
            this.btn_Next = new DevExpress.XtraEditors.SimpleButton();
            this.btn_First = new DevExpress.XtraEditors.SimpleButton();
            this.btn_Last = new DevExpress.XtraEditors.SimpleButton();
            this.labelControl1 = new DevExpress.XtraEditors.LabelControl();
            this.labelControl2 = new DevExpress.XtraEditors.LabelControl();
            this.textEdit1 = new DevExpress.XtraEditors.TextEdit();
            this.btn_Jump = new DevExpress.XtraEditors.SimpleButton();
            this.labelControl3 = new DevExpress.XtraEditors.LabelControl();
            this.comboBoxEdit1 = new DevExpress.XtraEditors.ComboBoxEdit();
            this.labelControl4 = new DevExpress.XtraEditors.LabelControl();
            ((System.ComponentModel.ISupportInitialize)(this.textEdit1.Properties)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.comboBoxEdit1.Properties)).BeginInit();
            this.SuspendLayout();
            // 
            // btn_Prev
            // 
            this.btn_Prev.Enabled = false;
            this.btn_Prev.Image = ((System.Drawing.Image)(resources.GetObject("btn_Prev.Image")));
            this.btn_Prev.Location = new System.Drawing.Point(271, 8);
            this.btn_Prev.Name = "btn_Prev";
            this.btn_Prev.Size = new System.Drawing.Size(29, 23);
            this.btn_Prev.TabIndex = 0;
            this.btn_Prev.MouseClick += new System.Windows.Forms.MouseEventHandler(this.simpleButton1_MouseClick);
            // 
            // btn_Next
            // 
            this.btn_Next.Image = ((System.Drawing.Image)(resources.GetObject("btn_Next.Image")));
            this.btn_Next.Location = new System.Drawing.Point(350, 8);
            this.btn_Next.Name = "btn_Next";
            this.btn_Next.Size = new System.Drawing.Size(29, 23);
            this.btn_Next.TabIndex = 1;
            this.btn_Next.MouseClick += new System.Windows.Forms.MouseEventHandler(this.btn_Next_MouseClick);
            // 
            // btn_First
            // 
            this.btn_First.Enabled = false;
            this.btn_First.Image = ((System.Drawing.Image)(resources.GetObject("btn_First.Image")));
            this.btn_First.Location = new System.Drawing.Point(236, 8);
            this.btn_First.Name = "btn_First";
            this.btn_First.Size = new System.Drawing.Size(29, 23);
            this.btn_First.TabIndex = 2;
            this.btn_First.MouseClick += new System.Windows.Forms.MouseEventHandler(this.btn_First_MouseClick);
            // 
            // btn_Last
            // 
            this.btn_Last.Image = ((System.Drawing.Image)(resources.GetObject("btn_Last.Image")));
            this.btn_Last.Location = new System.Drawing.Point(385, 8);
            this.btn_Last.Name = "btn_Last";
            this.btn_Last.Size = new System.Drawing.Size(29, 23);
            this.btn_Last.TabIndex = 3;
            this.btn_Last.MouseClick += new System.Windows.Forms.MouseEventHandler(this.btn_Last_MouseClick);
            // 
            // labelControl1
            // 
            this.labelControl1.Location = new System.Drawing.Point(15, 12);
            this.labelControl1.Name = "labelControl1";
            this.labelControl1.Size = new System.Drawing.Size(43, 14);
            this.labelControl1.TabIndex = 4;
            this.labelControl1.Text = "共 0 条,";
            // 
            // labelControl2
            // 
            this.labelControl2.Location = new System.Drawing.Point(178, 12);
            this.labelControl2.Name = "labelControl2";
            this.labelControl2.Size = new System.Drawing.Size(39, 14);
            this.labelControl2.TabIndex = 5;
            this.labelControl2.Text = "共 0 页";
            // 
            // textEdit1
            // 
            this.textEdit1.Location = new System.Drawing.Point(306, 9);
            this.textEdit1.Name = "textEdit1";
            this.textEdit1.Properties.Mask.EditMask = "\\d+";
            this.textEdit1.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.RegEx;
            this.textEdit1.Size = new System.Drawing.Size(38, 20);
            this.textEdit1.TabIndex = 6;
            this.textEdit1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.textEdit1_KeyUp);
            // 
            // btn_Jump
            // 
            this.btn_Jump.Location = new System.Drawing.Point(432, 8);
            this.btn_Jump.Name = "btn_Jump";
            this.btn_Jump.Size = new System.Drawing.Size(64, 23);
            this.btn_Jump.TabIndex = 7;
            this.btn_Jump.Text = "跳转";
            this.btn_Jump.Click += new System.EventHandler(this.btn_Jump_Click);
            // 
            // labelControl3
            // 
            this.labelControl3.Location = new System.Drawing.Point(67, 12);
            this.labelControl3.Name = "labelControl3";
            this.labelControl3.Size = new System.Drawing.Size(24, 14);
            this.labelControl3.TabIndex = 8;
            this.labelControl3.Text = "每页";
            // 
            // comboBoxEdit1
            // 
            this.comboBoxEdit1.Location = new System.Drawing.Point(94, 9);
            this.comboBoxEdit1.Name = "comboBoxEdit1";
            this.comboBoxEdit1.Properties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
            new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});
            this.comboBoxEdit1.Properties.Items.AddRange(new object[] {
            "1",
            "10",
            "20",
            "30",
            "60",
            "100",
            "全部"});
            this.comboBoxEdit1.Size = new System.Drawing.Size(47, 20);
            this.comboBoxEdit1.TabIndex = 9;
            this.comboBoxEdit1.SelectedIndexChanged += new System.EventHandler(this.comboBoxEdit1_SelectedIndexChanged);
            // 
            // labelControl4
            // 
            this.labelControl4.Location = new System.Drawing.Point(144, 12);
            this.labelControl4.Name = "labelControl4";
            this.labelControl4.Size = new System.Drawing.Size(12, 14);
            this.labelControl4.TabIndex = 10;
            this.labelControl4.Text = "条";
            // 
            // PageControl
            // 
            this.Controls.Add(this.labelControl4);
            this.Controls.Add(this.comboBoxEdit1);
            this.Controls.Add(this.labelControl3);
            this.Controls.Add(this.btn_Jump);
            this.Controls.Add(this.textEdit1);
            this.Controls.Add(this.labelControl2);
            this.Controls.Add(this.labelControl1);
            this.Controls.Add(this.btn_Last);
            this.Controls.Add(this.btn_First);
            this.Controls.Add(this.btn_Next);
            this.Controls.Add(this.btn_Prev);
            this.Name = "PageControl";
            this.Size = new System.Drawing.Size(547, 39);
            ((System.ComponentModel.ISupportInitialize)(this.textEdit1.Properties)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.comboBoxEdit1.Properties)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        /// <summary>
        /// 销毁控件
        /// </summary>
        /// <param name="disposing"></param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        /// <summary>
        /// 页面刷新方法
        /// </summary>
        public void PageRefresh()
        {
            if (this.DataRefresh != null)
            {
                this.DataRefresh(this, null);
                if ((this.PageCount == this.PageIndex) || (this.PageCount == 0))
                {
                    this.btn_Next.Enabled = false;
                    this.btn_Last.Enabled = false;
                }
                else
                {
                    this.btn_Next.Enabled = true;
                    this.btn_Last.Enabled = true;
                }
            }
        }

        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton1_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (this.PageIndex > 1)
            {
                int num = this.PageIndex - 1;
                this.PageIndex = num;
                this.PageRefresh();
            }

        }

        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Next_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (this.PageIndex < this.PageCount)
            {
                int num = this.PageIndex + 1;
                this.PageIndex = num;
                this.PageRefresh();
            }
        }

        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_First_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            this.PageIndex = 1;
            this.PageRefresh();
        }

        /// <summary>
        /// 尾页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Last_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            this.PageIndex = this.PageCount;
            this.PageRefresh();
        }

        /// <summary>
        /// 跳转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Jump_Click(object sender, EventArgs e)
        {
            this.PageRefresh();
        }

        /// <summary>
        /// 键盘按钮抬起响应事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textEdit1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (!string.IsNullOrEmpty(this.textEdit1.Text))
            {
                if (Convert.ToInt32(this.textEdit1.Text) > this._pageCount)
                {
                    this.PageIndex = this.PageCount == 0 ? 1 : this.PageIndex;
                }
                else
                {
                    this.PageIndex = Convert.ToInt32(this.textEdit1.Text);
                }
                if (e.KeyCode == Keys.Enter)
                {
                    this.PageRefresh();
                }
            }
        }

        /// <summary>
        /// 下拉框索引改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.PageIndex = 1;
            if (this.comboBoxEdit1.Text == "全部")
            {
                this._pageSize = 0x5f5e100;
            }
            else
            {
                try
                {
                    this._pageSize = Convert.ToInt32(this.comboBoxEdit1.Text);
                }
                catch
                {
                    this.comboBoxEdit1.SelectedIndex = 1;
                    this._pageSize = Convert.ToInt32(this.comboBoxEdit1.Text);
                }
            }
            this.PageRefresh();
        }

    }
}
